package com.ebupt.migu.music.system.service;

import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.tree.TreeBuilder;
import com.ebupt.migu.music.common.tree.TreeNode;
import com.ebupt.migu.music.system.entity.SysRole;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleService {
	@Autowired
	private RoleMapper roleMapper;
		
	/**
	 * 分页查询角色
	 * @param paramMap
	 * @return
	 */
	public Page<SysRole> queryByPage(Map<String,Object> paramMap){
		
		Page<SysRole> page = new Page<SysRole>();
		
		List<SysRole> list = roleMapper.queryByPage(paramMap);
		Integer total = roleMapper.queryCount(paramMap);
		
		page.setTotal(total);
		page.setData(list);
		
		return page;
	}

	/**
	 * 分页查询角色
	 * @param paramMap
	 * @return
	 */
	public List<SysUser> queryRoleUserById(String roleId){
		List<SysUser> list = roleMapper.queryRoleUserById(Integer.parseInt(roleId));
		return list;
	}
	
	/**
	 * 添加角色
	 * @param paramMap
	 * @return
	 */
	public String addRole(Map<String,Object> paramMap){
		try {
			roleMapper.addRole(paramMap);
			String roleId = paramMap.get("id").toString();

			String privs = "";

			if (paramMap.get("rolePrivIds") != null) {
				privs = paramMap.get("rolePrivIds").toString();

				String[] privArr = privs.split(",");

				for (String menuId:privArr) {
					Map<String,Object> privMap = new HashMap<String,Object>();

					privMap.put("roleId", roleId.toString());
					privMap.put("menuId", menuId);

					roleMapper.addRolePriv(privMap);
				}
			}

			return roleId;
		} catch (Exception e) {
			e.printStackTrace();

			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}
	
	/**
	 * 添加角色
	 * @param paramMap
	 * @return
	 */
	public String modifyRole(Map<String,Object> paramMap){
		try {
			//只能更新角色权限，不能更新角色名称
			roleMapper.modifyRole(paramMap);
			String roleId = paramMap.get("roleId").toString();

			String privs = "";

			if (paramMap.get("rolePrivIds") != null) {
				roleMapper.delRolePriv(paramMap);

				privs = paramMap.get("rolePrivIds").toString();

				String[] privArr = privs.split(",");

				for (String menuId:privArr) {
					Map<String,Object> privMap = new HashMap<String,Object>();

					privMap.put("roleId", roleId);
					privMap.put("menuId", menuId);

					roleMapper.addRolePriv(privMap);
				}
			}



			return roleId;
		} catch (Exception e) {
			e.printStackTrace();

			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}
	
	/**
	 * 删除角色
	 * @param paramMap
	 * @return
	 */
	public void delRole(Map<String,Object> paramMap){
		try {

			roleMapper.delRolePriv(paramMap);
			roleMapper.delRole(paramMap);

		} catch (Exception e) {
			e.printStackTrace();

			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}

	/**
	 * 获取角色已选中权限列表（树状结构）
	 * @param paramMap
	 * @return
	 */
	public List<TreeNode> getRolePrivList(Map<String,Object> paramMap){
		try {
			List<TreeNode> list = roleMapper.getRolePrivList(paramMap);
			List<TreeNode> tree = TreeBuilder.buildByRecursive(list);
			return tree;
		} catch (Exception e) {
			e.printStackTrace();

			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}

}
